<template>
  <div class="detail-content">
    <el-form :model="model" inline label-width="80px" :rules="validators">
      <el-row>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="model.name"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="model.age"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="model.sex">
            <el-option v-for="item in sexDict" :label="item.label" :value="item.code"> </el-option>
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="model.sex">
            <el-radio v-for="item in sexDict" :label="item.code">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="多选内容" prop="checkContent">
          <el-checkbox-group v-model="model.checkContent">
            <el-checkbox v-for="item in multiContentDict" :label="item.code">{{ item.label }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="账号" prop="account">
          <el-input v-model="model.account"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
  <footer class="detail-footer">
    <el-button @click="onConfirm" type="primary">确定</el-button>
    <el-button @click="onCancel">取消</el-button>
  </footer>
</template>
<script lang="ts" setup>
import { getDict } from '@/core/dict'
import { getDefault } from '@/model/demo'
import demoApi from '@/request/demo'
import { onMounted, reactive, ref } from 'vue'
const sexDict = getDict('sex')
const multiContentDict = getDict('multiContent')
const props = defineProps<{
  detailId: string
}>()
const validators = {
  name: [
    {
      required: true,
      message: '请输入名称',
    },
  ],
  age: [
    {
      required: true,
      message: '请输入年龄',
    },
    // 方便复制修改
    // {
    //   validator: (rule: any, value: string, callback: Function) => {
    //     let age = Number(value)
    //     if (isNaN(age)) {
    //       callback(new Error('请输入数字'))
    //       return
    //     }
    //     if (age < 0) {
    //       callback(new Error('年龄需要大于0'))
    //     } else {
    //       callback()
    //     }
    //   },
    //   trigger: 'blur',
    // },
  ],
}
const emit = defineEmits(['confirm', 'cancel'])
const model = ref(getDefault())
if (props.detailId) {
  model.value = await demoApi.getRow(props.detailId)
}
async function onConfirm() {
  let result = await demoApi.saveRow(model.value)
  console.log(result)
  emit('confirm', model.value)
}
function onCancel() {
  emit('cancel')
}
</script>
<style lang="less"></style>
